package com.yz.jdd.base.entity;


import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
 * Created by shibin on 2016/12/25.
 */
public class Response_Base<T> implements Parcelable {
    private String code;
    private String msg;
    private boolean success;

    protected T resultObject;

    public T getResultObject() {
        return resultObject;
    }

    public List<T> getList() {
        Gson gson = new Gson();
        return gson.fromJson(getResultObject().toString(), new TypeToken<List<T>>() {
        }.getType());
    }

    @Override
    public String toString() {
        return "{" +
                "code:\"" + code + "\"," +
                "msg:\"" + msg + "\"," +
                "resultObject:\"{" + resultObject + "}"
                + "}";
    }

    public Response_Base() {
    }

    protected Response_Base(Parcel in) {
        code = in.readString();
        msg = in.readString();
        success = in.readByte() != 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(code);
        dest.writeString(msg);
        dest.writeByte((byte) (success ? 1 : 0));
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Response_Base> CREATOR = new Creator<Response_Base>() {
        @Override
        public Response_Base createFromParcel(Parcel in) {
            return new Response_Base(in);
        }

        @Override
        public Response_Base[] newArray(int size) {
            return new Response_Base[size];
        }
    };

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public boolean success() {
        return success;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public void setResultObject(T resultObject) {
        this.resultObject = resultObject;
    }
}
